package com.hlf.common.utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class FTPImageTask {
    
    private static final Logger logger = LoggerFactory.getLogger(FTPImageTask.class);
    
    /**
     * Ftp服务器
     */
    private String server;
    /**
     * 用户名
     */
    private String uname;
 
    /**
     * 密码
     */
    private String password;
    /**
     * 连接端口，默认21
     */
    private int port = 21;
    /**
     * basePath
     */

    private String storePath;

    private static InputStream inStream = null;

    private static Properties property = null;
    public void execute(String localAdr,String remoteAdr){
        try {
            ClassLoader loader = FtpHelper.class.getClassLoader();
            inStream = loader.getResourceAsStream("config.properties");
            setArg(inStream);
            FTPUtils ftp = new FTPUtils();

            ftp.asyncUploadFile(server, port, uname, password,localAdr,storePath +"/"+remoteAdr);
            //ftpHelper.uploadFile(localAdr, ftpHelper.getStorePath() + "/" + remoteAdr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void execute(InputStream input ,String remoteAdr) throws IOException{

        ClassLoader loader = FtpHelper.class.getClassLoader();
        inStream = loader.getResourceAsStream("config.properties");
        setArg(inStream);
        FTPUtils ftp = new FTPUtils();
        ftp.uploadFile(server, port, uname, password,input,storePath +"/"+remoteAdr);
    }

    public boolean removeImg(String remoteAdr){
        ClassLoader loader = FtpHelper.class.getClassLoader();
        inStream = loader.getResourceAsStream("config.properties");
        setArg(inStream);
        FTPUtils ftp = new FTPUtils();
        logger.info(storePath + remoteAdr);
        return ftp.deleteFile(server, port, uname, password,storePath +"/"+ remoteAdr);
    }

    public boolean removeOrderImg(String remoteAdr){
        ClassLoader loader = FtpHelper.class.getClassLoader();
        inStream = loader.getResourceAsStream("config.properties");
        setArg(inStream);
        FTPUtils ftp = new FTPUtils();
        return ftp.deleteFile(server, port, uname, password,storePath +"/"+remoteAdr);
    }



    /**
     * 设置参数
     *
     * @param configFile
     *            --参数的配置文件
     */
    private  void setArg(InputStream instream) {
        property = new Properties();
        BufferedInputStream inBuff = null;
        try {
            inBuff = new BufferedInputStream(instream);
            property.load(inBuff);
            uname = property.getProperty("username");
            password = property.getProperty("password");
            server = property.getProperty("ip");
            storePath = property.getProperty("storePath");
            port = Integer.parseInt(property.getProperty("port"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inBuff != null)
                    inBuff.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
